package com.guilin.system.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.guilin.common.exception.ServiceException;
import com.guilin.common.utils.DateUtils;
import com.guilin.common.utils.uuid.IdUtils;
import com.guilin.domain.SurveyQuestion;
import com.guilin.domain.SurveyQuestionnaire;
import com.guilin.domain.SurveyResponseDetail;
import com.guilin.mapper.SurveyResponseDetailMapper;
import com.guilin.system.service.SurveyQuestionService;
import com.guilin.system.service.SurveyQuestionnaireService;
import com.guilin.system.service.SurveyResponseDetailService;
import com.guilin.system.service.SurveyResponseService;
import com.guilin.vo.SurveyResponseSubmitVO;
import com.guilin.vo.SurveyResponseVO;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.guilin.mapper.SurveyResponseMapper;
import com.guilin.domain.SurveyResponse;
import org.springframework.transaction.annotation.Transactional;


/**
 * 问卷调查记录Service业务层处理
 * 
 * @author xiaoqi
 * @date 2025-06-24
 */
@Service
public class SurveyResponseServiceImpl implements SurveyResponseService
{
    @Autowired
    private SurveyResponseMapper surveyResponseMapper;

    @Autowired
    private SurveyResponseDetailMapper surveyResponseDetailMapper;


    /**
     * 查询问卷调查记录
     * 
     * @param id 问卷调查记录主键
     * @return 问卷调查记录
     */
    @Override
    public SurveyResponseVO selectSurveyResponseById(Long id)
    {
        return surveyResponseMapper.selectSurveyResponseById(id);
    }

    /**
     * 查询问卷调查记录列表
     * 
     * @param surveyResponse 问卷调查记录
     * @return 问卷调查记录
     */
    @Override
    public List<SurveyResponseVO> selectSurveyResponseList(SurveyResponse surveyResponse)
    {
        SurveyResponseVO surveyResponseVO = new SurveyResponseVO();
        BeanUtils.copyProperties(surveyResponse, surveyResponseVO);
        return surveyResponseMapper.selectSurveyResponseList(surveyResponseVO);
    }

    /**
     * 新增问卷调查记录
     * 
     * @param surveyResponse 问卷调查记录
     * @return 结果
     */
    @Override
    public int insertSurveyResponse(SurveyResponse surveyResponse)
    {
        // 生成问卷调查编号
        String responseCode = IdUtils.fastSimpleUUID();
        surveyResponse.setResponseCode(responseCode);

        surveyResponse.setCreateTime(DateUtils.getNowDate());
        return surveyResponseMapper.insertSurveyResponse(surveyResponse);
    }

    /**
     * 修改问卷调查记录
     * 
     * @param surveyResponse 问卷调查记录
     * @return 结果
     */
    @Override
    public int updateSurveyResponse(SurveyResponse surveyResponse)
    {
        surveyResponse.setUpdateTime(DateUtils.getNowDate());
        return surveyResponseMapper.updateSurveyResponse(surveyResponse);
    }

    /**
     * 批量删除问卷调查记录
     * 
     * @param ids 需要删除的问卷调查记录主键
     * @return 结果
     */
    @Override
    public int deleteSurveyResponseByIds(Long[] ids)
    {
        return surveyResponseMapper.deleteSurveyResponseByIds(ids);
    }

    /**
     * 删除问卷调查记录信息
     * 
     * @param id 问卷调查记录主键
     * @return 结果
     */
    @Override
    public int deleteSurveyResponseById(Long id)
    {
        return surveyResponseMapper.deleteSurveyResponseById(id);
    }


    /**
     * 用户提交问卷
     * @param submitVO
     * @return
     */
    @Transactional
    public Long insertUserResponse(SurveyResponseSubmitVO submitVO) {

        // 1. 创建回答记录
        SurveyResponse response = new SurveyResponse();
        response.setResponseCode(IdUtils.fastSimpleUUID()); // 生成唯一回答编号
        response.setUserId(submitVO.getUserId());
        response.setQuestionnaireId(submitVO.getQuestionnaireId());
        response.setResponseTime(new Date());
        response.setCreateTime(new Date());

        // 2. 保存回答记录
        int count = surveyResponseMapper.insertSurveyResponse(response);
        if (count <= 0) {
            throw new ServiceException("问卷提交失败");
        }

        // 3. 保存回答详情
        for (SurveyResponseSubmitVO.AnswerDetail answer : submitVO.getAnswers()) {
            SurveyResponseDetail detail = new SurveyResponseDetail();
            detail.setResponseId(response.getId());
            detail.setUserId(submitVO.getUserId());
            detail.setQuestionId(answer.getQuestionId());
            detail.setCreateTime(new Date());
            detail.setUpdateTime(new Date());

            // 根据提交的数据设置不同字段
            if (answer.getTextAnswer() != null) {
                detail.setTextAnswer(answer.getTextAnswer());
            }
            if (answer.getNumericAnswer() != null) {
                detail.setNumericAnswer(Long.valueOf(answer.getNumericAnswer()));
            }
            if (answer.getOptionValues() != null && !answer.getOptionValues().isEmpty()) {
                detail.setOptionValues(String.join(",", answer.getOptionValues()));
            }

            surveyResponseDetailMapper.insertSurveyResponseDetail(detail);
        }

        return response.getId();
    }
}
